package com.medaxis.hpams.sso.controller;

import com.medaxis.hpams.dto.global.BaseResponse;
import com.medaxis.hpams.dto.global.ErrorException;
import com.medaxis.hpams.dto.global.SuccessResponse;
import com.medaxis.hpams.sso.constant.Constants;
import com.medaxis.hpams.sso.domain.dto.GroupDto;
import com.medaxis.hpams.sso.domain.dto.TableDataInfo;
import com.medaxis.hpams.sso.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门Controller
 */
@RestController
@RequestMapping(Constants.VERSION_NUMBER + "/sso/group")
@Api(tags = Constants.VERSION_NUMBER + "分组管理")
public class GroupController extends BaseController {
    @Autowired
    private GroupService groupService;

    /**
     * 查询组织列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemId", value = "系统归属", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/list")
    @ApiOperation(value = "查询组织列表", httpMethod = "GET")
    public BaseResponse<TableDataInfo> list(GroupDto GroupDto) {
        startPage();
        List<GroupDto> list = groupService.selectGroupList(GroupDto);
        return SuccessResponse.success(getDataTable(list));
    }

    /**
     * 获取部门详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据组织ID获取部门详情信息", httpMethod = "GET")
    public BaseResponse getInfo(@PathVariable Long id) {
        return SuccessResponse.success(groupService.selectGroupByGroupId(id));
    }

    /**
     * 新增组织
     */
    @PostMapping
    @ApiOperation(value = "新增组织", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemId", value = "系统归属", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "组织状态", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "组织名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父组织Id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNum", value = "排序", required = true, dataType = "String", paramType = "query"),
    })
    public BaseResponse add(GroupDto groupDto) {
        if (Constants.NOT_UNIQUE.equals(groupService.checkUnique(groupDto))) {
            throw new ErrorException(Constants.GROUP_REPEAT_NAME);
        }
         // todo 添加创建人
            groupDto.setCreateBy("admin");
            return toBaseResponse(groupService.insertGroup(groupDto));
    }

        /**
         * 修改组织
         */
        @PutMapping
        @ApiOperation(value = "修改组织", httpMethod = "PUT")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "parentId", value = "父组织Id", required = true, dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "id", value = "组织id", required = true, dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "name", value = "组织名称", required = true, dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "ancestors", value = "祖籍列表", required = true, dataType = "String", paramType = "query"),
        })
        public BaseResponse edit (GroupDto groupDto)
        {
//        组织名称不能重复
            if (Constants.NOT_UNIQUE.equals(groupService.checkUnique(groupDto))) {

                throw new ErrorException(Constants.GROUP_REPEAT_NAME);
            }
//         父级组织不能是自己
            if (groupDto.getId().equals(groupDto.getParentId())) {
                throw new ErrorException(Constants.GROUP_ID_NOT_MYSELF);
            }
            // todo 设置维护人
            groupDto.setUpdateBy("admin");
            return toBaseResponse(groupService.updateGroup(groupDto));
        }

        /**
         * 删除组织
         */
        @DeleteMapping("/{ids}")
        @ApiOperation(value = "删除组织", httpMethod = "DELETE")
        public BaseResponse remove (@PathVariable Long[] ids)
        {
            return toBaseResponse(groupService.deleteGroupByGroupIds(ids));
        }
    }
